package com.goldcn.user.controller;

import com.goldcn.user.model.Role;
import com.goldcn.user.model.RoleFunction;
import com.goldcn.user.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 1111 on 2016/11/15.
 */
@Api(value = "角色管理")
@RestController
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @ApiOperation(value = "新增角色")
    @RequestMapping(method = RequestMethod.POST)
    public Object addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }

    @ApiOperation(value = "修改角色")
    @RequestMapping(value = "",method = RequestMethod.PUT)
    public Object updateRole(@RequestBody Role role){
        return roleService.updateRole(role);
    }

    @ApiOperation(value = "判断用户名是否存在[0存在，1不存在]")
    @RequestMapping(value = "/roleName",method = RequestMethod.POST)
    public Object judgeRoleName(@RequestBody List<String> roleName){
        return roleService.judgeRoleName(roleName.get(0));
    }

    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Object deleteRole(@PathVariable Long id){
        return roleService.deleteRole(id);
    }

    @ApiOperation(value = "按id查询角色")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Object selectRoleByID(@PathVariable Long id){
        return roleService.selectByRoleID(id);
    }

    @ApiOperation(value = "查询所有角色列表[包括所属权限信息]")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Object selectRoleList(){
        return roleService.roleList();
    }

    @ApiOperation(value = "查询角色的功能权限")
    @RequestMapping(value = "/function/{roleID}",method = RequestMethod.GET)
    public Object selectFunctionByRoleID(@PathVariable Long roleID){
        return roleService.selectFunctionByRoleID(roleID);
    }

    @ApiOperation(value = "更新角色的功能权限")
    @RequestMapping(value = "/function/{roleID}",method = RequestMethod.PUT)
    public Object updateRoleFunction(@PathVariable Long roleID,@RequestBody List<RoleFunction> rfList){
        return roleService.updateRoleFunction(roleID,rfList);
    }

}
